﻿@using ABC.Controllers.ViewModels.My
@using ABC.Domain.Entities
@model FinancialPolicyViewModel
@{
    ViewBag.Title = "Финансовая политика - Азбука24";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
    var company = Request.Params["company"];
    var type = Request.Params["type"];
    var creditP = Request.Params["credit"];
    var isarchive = Request.Params["isarchive"];
    var pagesize = Request.Params["pagesize"];
}
<script type="text/javascript">
    function ChangeId() {
        Filter();
    }
    function ResetForm() {
        $("#form1 #tab1 input").val("").attr("checked", false);
        $("#form1 #tab2 input").val("").attr("checked", false);
        $("#form1 #tab3 input").val("").attr("checked", false);
        $("#form1 #tab4 input").attr("checked", false);
        $("#form1 select").val("");
        $("#PolicyItem_Id").val("");
        $("#companyid").val($("#Company").val());
        $("#fin-record").modal({ "show": "true", dynamic: true });
    }
    function Filter() {
        location.href = '@Url.Action("Finance", "Admin")' + "?company=" + $("#Company").val() + ($("#Type").val() ? "&type=" + $("#Type").val() : "") + ($("#Credit").val() ? "&credit=" + $("#Credit").val() : "") + ($("#IsArchive").is(":checked") ? "&isarchive=" + $("#IsArchive").is(":checked") : "") + ($("#PageSize").val() != '10' ? "&pagesize=" + $("#PageSize").val() : "");
    }
    function CopyOrArchiveRecord(obj) {
        var chk = $(obj).parents("tr").children("td").children("input[type='checkbox']");
        var form1 = document.createElement('form');
        form1.action = '@Url.Action("CopyOrArchiveRecord", "Admin")';
        form1.method = 'post';
        form1.acceptCharset = 'utf-8';
        form1.appendChild(CreateHiddenField('id', chk.val()));
        form1.appendChild(CreateHiddenField('companyid', $("[name='companyid']").val()));
        form1.appendChild(CreateHiddenField('company', $("#company").val()));
        form1.appendChild(CreateHiddenField('type', $("#type").val()));
        form1.appendChild(CreateHiddenField('credit', $("#credit").val()));
        form1.appendChild(CreateHiddenField('page', $("#page").val()));
        form1.appendChild(CreateHiddenField('pagesize', $("#pagesize").val()));
        form1.appendChild(CreateHiddenField('isarchive', $("#isarchive").val()));
        form1.appendChild(CreateHiddenField('to', $(obj).attr("to")));
        document.body.appendChild(form1);
        form1.submit();
    }

    function ResetFilter() {
        location.href = '@Url.Action("Finance", "Admin")';
    }
    function GetRecords() {
        Filter();
    }
    function DeleteRecords() {
        var postform = document.getElementById("form2");
        postform.action = '@Url.Action("DeleteRecords", "Admin")';
        postform.submit();
    }
    function EditRecord(obj) {
        var chk = $(obj).parents("tr").children("td").children("input[type='checkbox']");
        $.ajax({
            url: '@Url.Action("GetPolicyItem", "Admin")',
            type: "POST",
            data: { companyId: $("#Company").val(), guidId: chk.val() },
            success: function (data) {
                if (data.Id) {
                    if (data.IsArchive == true) {
                        $("#PolicyItem_IsArchive").attr("checked", "checked");
                    }
                    $("#PolicyItem_TypeOfInsurance").val(data.TypeOfInsurance);
                    for (var i = 0; i < data.Rewards.length; i++) {
                        $("input[name='" + data.Rewards[i].Id + "']").val(data.Rewards[i].Value);
                    }
                    $("#timebegin").val(data.DateBeginAction);
                    $("#timeend").val(data.DateEndAction);
                    for (var j = 0; j < data.Credits.length; j++) {
                        if (data.Credits[j].Value == "True") {
                            $("input[value='" + data.Credits[j].Id + "']").attr("checked", "checked");
                        }
                    }
                    for (var k = 0; k < data.SpecialPrograms.length; k++) {
                        if (data.SpecialPrograms[k].Value == "True") {
                            $("input[value='" + data.SpecialPrograms[k].Id + "']").attr("checked", "checked");
                        }
                    }
                    $("#PolicyItem_Id").val(data.Id);
                    $("#PolicyItem_Comment").val(data.Comment);
                    $("#fin-record").modal({ "show": "true", dynamic: true });
                }
            }
        });
    }
    function ChangePageSize() {
        Filter();
    }
    function LoadProducts() {
        $.getJSON('@Url.Action("GetProducts", "Admin", new { area = "" })' + "?partnerId=" + $("#PolicyItem_Company").val() + "&typeId=" + $("#PolicyItem_TypeOfInsurance").val(), function (products) {
            var selElement = document.getElementById("PolicyItem_Product");
            var selElementLength = selElement.options.length;
            for (var i = 0; i < selElementLength; i++) {
                selElement.remove(selElement.options[i]);
            }
            for (var j = 0; j < products.length; j++) {
                selElement.options[j] = new Option(products[j].Text, products[j].Value);
            }
        });
    }
</script>
<div class="row-fluid">
    <div class="page-header">
        <h2>
            Финансовая политика</h2>
    </div>
    <div class="row-fluid">
        <div class="span2">
            <div class="control-group">
                <label class="control-label" for="Company">
                    Страховая компания</label>
                <div class="controls">
                    @Html.DropDownListFor(x => x.Company, Model.ListForCompany, new { @class = "input-medium", onchange = "ChangeId();" })
                </div>
            </div>
        </div>
        <div class="span2">
            <div class="control-group">
                <label class="control-label" for="Type">
                    Вид страхования</label>
                <div class="controls">
                    @Html.DropDownListFor(x => x.Type, Model.ListForType, "Все виды", new { @class = "input-medium" })
                </div>
            </div>
        </div>
        <div class="span2">
            <div class="control-group">
                <label class="control-label" for="Credit">
                    Кредит</label>
                <div class="controls">
                    @Html.DropDownListFor(x => x.Credit, Model.ListForCredit, "Все банки", new { @class = "input-medium" })
                </div>
            </div>
        </div>
        <div class="span2">
            <div class="control-group">
                <label class="control-label" for="IsArchive">
                    Режим отображения</label>
                <div class="controls">
                    <label class="checkbox">
                        @Html.CheckBoxFor(x => x.IsArchive)
                        Архивные записи
                    </label>
                </div>
            </div>
        </div>
        <div class="span2">
        </div>
        <div class="span2">
            <div class="control-group">
                <label class="control-label" for="phone-01">
                    Фильтр</label>
                <div class="controls">
                    <div class="btn-group">
                        <button type="button" class="btn" onclick="GetRecords();">
                            Применить</button>
                        <button class="btn" rel="tooltip" onclick="ResetFilter();" data-original-title="Сбросить фильтр">
                            <i class="icon-refresh"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <table class="table table-bordered table-striped">
        <thead>
            <tr>
                <th class="col-1" rowspan="2">
                    <input type="checkbox" id="inlineCheckbox01" onchange="ChangeAllCheck(this);" value="selectall">
                </th>
                <th rowspan="2">
                    Вид
                </th>
                <th rowspan="2">
                    Продукт
                </th>
                <th colspan="@Model.Rewards.Count">
                    Комиссионное вознаграждение
                </th>
                <th colspan="2">
                    Период действия
                </th>
                <th colspan="2">
                    Условие
                </th>
                <th rowspan="2">
                    Инструменты
                </th>
            </tr>
            <tr>
                @foreach (var reward in Model.Rewards)
                {
                    <th>
                        @reward.Name
                    </th>
                }
                <th>
                    Начало
                </th>
                <th>
                    Окончание
                </th>
                <th>
                    Кредит
                </th>
                <th>
                    Комментарий
                </th>
            </tr>
        </thead>
        <tbody>
            @using (Html.BeginForm("ToArchive", "Admin", FormMethod.Post, new { id = "form2" }))
            {
                @Html.Hidden("companyid", string.IsNullOrEmpty(company) ? Model.ListForCompany.First().Value : company, new { id = "company_id" })
                @Html.Hidden("company")
                @Html.Hidden("type")
                @Html.Hidden("credit")
                @Html.Hidden("isarchive")
                @Html.Hidden("pagesize", pagesize == "0" ? Model.ListForPageSize.First().Value : pagesize)
                @Html.Hidden("page")
                foreach (var item in Model.Finance)
                {
                <tr>
                    <td>
                        <input type="checkbox" onchange="ChangeCheck();" name="policyitem" value="@item.Id">
                    </td>
                    <td>
                        @Model.ListForType.FirstOrDefault(x => item.TypeOfInsurance == x.Value).Text
                    </td>
                    <td>
                        @item.Product
                    </td>
                    @foreach (var t in Model.Rewards)
                    {
                        <td>
                            @(item.Rewards.Any(x => x.Id == t.Id) ? item.Rewards.FirstOrDefault(x => x.Id == t.Id).Value + "%" : "")
                        </td>
                    }
                    <td>
                        @item.DateBeginAction
                    </td>
                    <td>
                        @item.DateEndAction
                    </td>
                    <td>
                        <ul class="unstyled">
                            @foreach (var credit in item.Credits)
                            {
                                <li>@(Boolean.Parse(credit.Value) ? credit.Name : "")</li>
                            }
                        </ul>
                    </td>
                    <td>
                        @item.Comment
                    </td>
                    <td>
                        <div class="btn-group">
                            <button class="btn" data-toggle="modal" onclick="EditRecord(this);" rel="tooltip"
                                type="button" title="Редактировать">
                                <i class="icon-edit"></i>
                            </button>
                            <button class="btn" rel="tooltip" to="copy" onclick="CopyOrArchiveRecord(this);"
                                type="button" title="Создать копию">
                                <i class="icon-plus"></i>
                            </button>
                            <button class="btn" rel="tooltip" to="archive" onclick="CopyOrArchiveRecord(this);"
                                type="button" title="Перенести в архив">
                                <i class="icon-lock"></i>
                            </button>
                        </div>
                    </td>
                </tr>
                }
            }
        </tbody>
        <tfoot>
            <tr>
                <td colspan="100%">
                    <div class="table-toolbar">
                        <div class="quant-item pull-left">
                            @Html.DropDownListFor(x => x.PageSize, Model.ListForPageSize, new { @class = "input-mini", onchange = "ChangePageSize();" })
                            <span class="help-inline">записей на странице</span>
                        </div>
                        <div class="pagination pagination-right">
                            <ul>
                                <!-- Если активна первая страница, то этот пункт не показывать -->
                                @Html.Paging(Model.PageNumber, Model.PageCount, i => Url.Action("Finance", "Admin", new { page = i, company, type, credit = creditP, isarchive, pagesize }))
                                <!-- Если активна последняя страница, то этот пункт не показывать -->
                            </ul>
                        </div>
                    </div>
                </td>
            </tr>
        </tfoot>
    </table>
    <button class="btn btn-success" data-toggle="modal" onclick="ResetForm();">
        Добавить запись</button>
    <button class="btn" onclick="$('#form2').submit();" state="watch" disabled="disabled">
        Перенести в архив</button>
    <button class="btn" onclick="DeleteRecords();" state="watch" disabled="disabled">
        Удалить</button>
</div>
<!--// Модальное окно выбора котировки -->
<div class="modal hide " id="fin-record">
    <div class="modal-header">
        <button data-dismiss="modal" class="close">
            ×</button>
        <h3>
            Добавить запись финансовой политики</h3>
    </div>
    <div class="modal-body">
        @using (Html.BeginForm("Finance", "Admin", FormMethod.Post, new { @class = "form-horizontal", id = "form1" }))
        {
            @Html.Hidden("companyid", string.IsNullOrEmpty(company) ? Model.ListForCompany.First().Value : company)
            @Html.HiddenFor(x => x.PolicyItem.Id)
            @Html.Hidden("company")
            @Html.Hidden("type")
            @Html.Hidden("credit")
            @Html.Hidden("isarchive")
            @Html.Hidden("pagesize")
            @Html.Hidden("page")
            <div class="tabbable">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#tab1" data-toggle="tab">Основные данные</a></li>
                    <li class=""><a href="#tab2" data-toggle="tab">Комиссия</a></li>
                    <li class=""><a href="#tab3" data-toggle="tab">Период действия</a></li>
                    <li class=""><a href="#tab4" data-toggle="tab">Условия</a></li>
                </ul>
                <div class="tab-content">
                    <div class="tab-pane  active" id="tab1">
                        <div class="control-group">
                            <label class="control-label" for="IsArchive">
                                Состояние</label>
                            <div class="controls">
                                <label class="checkbox">
                                    @Html.CheckBoxFor(x => x.PolicyItem.IsArchive)
                                    Архивная запись
                                </label>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="PolicyItem_Company">
                                Страховая компания</label>
                            <div class="controls">
                                @Html.DropDownListFor(x => x.PolicyItem.Company, Model.ListForCompany, new { @class = "input-medium", onchange = "LoadProducts();" })
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="PolicyItem_TypeOfInsurance">
                                Вид</label>
                            <div class="controls">
                                @Html.DropDownListFor(x => x.PolicyItem.TypeOfInsurance, Model.ListForType, new { @class = "input-medium", onchange = "LoadProducts();" })
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="PolicyItem_Product">
                                Продукт</label>
                            <div class="controls">
                                @Html.DropDownListFor(x => x.PolicyItem.Product, Model.ListForProduct, new { @class = "input-large" })
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane" id="tab2">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>
                                        Объект
                                    </th>
                                    <th>
                                        КВ
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var reward in Model.Rewards)
                                {
                                    <tr>
                                        <td>
                                            @reward.Name
                                        </td>
                                        <td>
                                            <input type="text" class="input-mini" name="@reward.Id">
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                    <div class="tab-pane" id="tab3">
                        <div class="control-group">
                            <label class="control-label" for="timebegin">
                                Начало</label>
                            <div class="controls">
                                @Html.TextBoxFor(x => x.PolicyItem.DateBeginAction, new { @class = "input-small date", id = "timebegin" })
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="timeend">
                                Окончание</label>
                            <div class="controls">
                                @Html.TextBoxFor(x => x.PolicyItem.DateEndAction, new { @class = "input-small date", id = "timeend" })
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane" id="tab4">
                        <div class="tabbable tabs-left">
                            <ul class="nav nav-tabs">
                                <li class="active"><a href="#tab-2-1" data-toggle="tab">Кредит</a></li>
                                <li class=""><a href="#tab-2-2" data-toggle="tab">Спецпрограмма</a></li>
                                <li class=""><a href="#tab-2-5" data-toggle="tab">Комментарий</a></li>
                            </ul>
                            <div class="tab-content">
                                <div class="tab-pane active" id="tab-2-1">
                                    @foreach (var credit in Model.Credits)
                                    {
                                        <label class="checkbox">
                                            <input type="checkbox" name="selectcredits" value="@credit.Id">
                                            @credit.Name
                                        </label>
                                    }
                                </div>
                                <div class="tab-pane" id="tab-2-2">
                                    @foreach (var program in Model.SpecialPrograms)
                                    {
                                        <label class="checkbox">
                                            <input type="checkbox" name="specprograms" value="@program.Id">
                                            @program.Name
                                        </label>
                                    }
                                </div>
                                <div class="tab-pane" id="tab-2-5">
                                    @Html.TextAreaFor(x => x.PolicyItem.Comment, new { @class = "textarea-large finance-comment", placeholder = "Здесь следует указать условия, которые невозможно классифицировать." })
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" onclick="$('#form1').submit();">
            Сохранить</button>
        <button data-dismiss="modal" class="btn">
            Отменить</button>
    </div>
</div>
<!-- Модальное окно выбора котировки //-->
<script type="text/javascript">
    allCheckboxes = $("input[name='policyitem']");
</script>
